package tom_io_nio.nio.buffer;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;

public class MappedBuffer {

    static private final int start = 0;
    static private final int size = 1024;

    public static void main(String[] args) throws Exception{

        RandomAccessFile file = new RandomAccessFile("/Users/mac/Desktop/test.txt","rw");

        FileChannel fc = file.getChannel();

        //把缓冲区和文件进行一个映射关联
        //操作缓冲区会对应改变文件
        MappedByteBuffer mappedBuffer = fc.map(FileChannel.MapMode.READ_WRITE,start,size);

        mappedBuffer.put(8,(byte)97);
        mappedBuffer.put(20,(byte)98);

        file.close();
        fc.close();
    }
}
